package com.qf.myschool.servlet;

import org.omg.CORBA.Request;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.Member;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 *
 * @version 1.0
 * @auth zgy
 * @date 2020/4/13
 */
@WebServlet("/SuperServlet")
public class SuperServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置字符编码
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        //接受请求中的方法名
        String methodName=request.getParameter("method");
        //获得当前请求sevelet的放射对象，this指当前请求的servelet
        Class servletClass=this.getClass();
        //通过方法名得到反射对象
        try {
            Method methodClass=servletClass.getDeclaredMethod(methodName,HttpServletRequest.class,HttpServletResponse.class);
            methodClass.setAccessible(true);
            //用方法的反射对象调用相对应的方法，并接受返回的结果，以便之后的处理，this指请求的servlet
            String result=methodClass.invoke(this,request,response).toString();
            //根据返回的字符串来判定是转发，重定向还是ajax
            //result="url"  转发
            //result="redirect:url"  重定向
            //result="ajax:String"  响应字符串给ajax
            if(result.startsWith("redirect:")){//重定向
                String url=result.substring(result.indexOf(":")+1);
                response.sendRedirect(url);
            }else if(result.startsWith("ajax:")){
                String content=result.substring(result.indexOf(":")+1);
                    response.getWriter().write(content);
                }else {  //z转发
                    request.getRequestDispatcher(result).forward(request,response);
                }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public <T> T requestObject(Class<T> clazz,HttpServletRequest Request,HttpServletResponse response) throws IllegalAccessException, InstantiationException {
            //获得请求对象中所有数据
            Map<String,String[]>requests=Request.getParameterMap();
            //创建一个集合存请求数据处理后的数据
            Map<String,String>hmap=new HashMap<>();
            //遍历原来集合，把数据处理后存入新的集合
            for(String key:requests.keySet()){
                if(key!=null){
                    //将原来数据的value值从String[]类型转化为String类型
                    String newValue= Arrays.toString(requests.get(key));
                    hmap.put(key,newValue.substring(1,newValue.length()-1));
                }
            }
            T t1=clazz.newInstance();
            //用反射对象调用方法得到所有的属性反射对象
            Field fields[]=clazz.getDeclaredFields();
            //遍历所有属性的反射对象，将属性值设置到实例中
            for(Field f:fields){
                if(hmap.get(f.getName())!=null&&!hmap.get(f.getName()).equals("")){
                    f.setAccessible(true);
                    f.set(t1,hmap.get(f.getName()));
                }
            }
            return t1;
    }
}
